// e39.groovy
/*
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p <= 1000, is the number of solutions maximised?
*/
maxcount = 0
maxp = 0
for (p in (120..1000).step(2)) {
    def count = 0
    for (a in 1..(p/3)) {
        for (b in 1..((p-a)/2)) {
            def c = p-a-b
            if (a*a + b*b == c*c) { count++ }
        }
    }
    if (count > maxcount) {
        maxcount = count
        maxp = p
    }
}
println "Max: $maxp Count: $maxcount"
